<?php
namespace Admin\Controller;
use Think\Controller;
class SysController extends Controller {

    private $modelName = 'Sys';

    public function index(){
        $modelObj = M($this->modelName);
        $list = $modelObj->select();
        if (!empty($list) && count($list) > 0) {
            $this->assign('sys',$list[0]);
        }

        $modelObj = M("Game_type");
        $list = $modelObj->where(array("state"=>1))->select();
        if (!empty($list) && count($list) > 0) {
            $this->assign('games',$list);
        }

        $modelObj = M('VoucherType');
        $list = $modelObj->select();
        $this->assign('voucher_type_list',$list);

        $this->display();
    }

    public function setting() {
        $modelObj = M($this->modelName);
        $data = $modelObj->create($_POST);
        $modelObj->save($data);

        $ajaxdata['code'] = 0;
        $ajaxdata['msg'] = '设置保存成功';
        $this->ajaxReturn($ajaxdata);
    }

    public function notice() {
        $this->display();
    }

    public function tonoticelist() {
        $page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $model = M("SysNotice");
        $list = $model->order('datetime desc')
            ->page($page_num, $page_size)
            ->select();

        $total = $model->count('id');
        $data = pageHelper($page_size, $page_num, $list, $total);
        $this->ajaxReturn($data);
    }

    public function deletenotice() {
        $modelObj = M("SysNotice");
        $map['id'] = $_POST['id'];
        $modelObj->where($map)->delete();

        $data['code'] = 0;
        $this->ajaxReturn($data);
    }

    public function addoreditnotice() {
        $modelObj = M("SysNotice");
        $data = $modelObj->where(array('id' => $_GET['id']))->find();
        //$data['id'] = 1;

        $this->assign('data', $data);
        $this->display();
    }

    public function addoreditnoticedata() {
        //$data['id'] = $_POST['id'];
        $modelObj = M("SysNotice");
        $id = $_POST['id'];
        $ajaxdata['code'] = 0;
        if (empty($_POST['id'])) {
            $data = $modelObj->create($_POST);
            //数据校验
            if (empty($data['title'])) {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '未填写公告标题';
                $this->ajaxReturn($ajaxdata);
            }
            if (empty($data['content'])) {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '未填写公告内容';
                $this->ajaxReturn($ajaxdata);
            }
            //管理员账号不存在，添加管理员
            $data['datetime'] = date('Y-m-d H:i:s');
            $id = $modelObj->add($data);
            $ajaxdata['msg'] = '添加成功';
        } else {
            $manager = M('SysNotice');
            $data = $manager->create($_POST);
            $manager->save($data);
            $ajaxdata['msg'] = '修改成功';
        }

        $this->ajaxReturn($ajaxdata);
    }
}